package com.example.demo_yunpan.service;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;

public class HadoopConnectivityTest {

    public static void main(String[] args) {
        // 使用提供的HDFS集群名称
        String hdfsUri = "hdfs://hadoop1:9000";

        Configuration configuration = new Configuration();
        // 设置HDFS集群的地址，这里使用了HDFS的高可用性名称
        configuration.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
        configuration.set("fs.defaultFS", hdfsUri);
        // 如果需要，可以添加其他Hadoop配置

        try {
            // 获取FileSystem实例
            FileSystem fileSystem = FileSystem.get(configuration);

            // 检查根目录是否存在，作为连通性测试
            Path rootPath = new Path("/");
            if (fileSystem.exists(rootPath)) {
                System.out.println("Successfully connected to HDFS at " + hdfsUri);
                System.out.println("HDFS Root Path Exists: " + rootPath);
                // 列出HDFS某个目录下的所有文件
                listFilesInDirectory(fileSystem, new Path("/tmp")); // 替换为您要列出文件的HDFS目录
            } else {
                System.out.println("Failed to connect to HDFS or the root path does not exist.");
            }

            // 关闭FileSystem连接
            fileSystem.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("Error connecting to HDFS: " + e.getMessage());
        }
    }
    public static void listFilesInDirectory(FileSystem fileSystem, Path directory) throws IOException {
        FileStatus[] fileStatuses = fileSystem.listStatus(directory);
        for (FileStatus fileStatus : fileStatuses) {
            if (fileStatus.isDirectory()) {
                System.out.println("Directory: " + fileStatus.getPath());
                listFilesInDirectory(fileSystem, fileStatus.getPath()); // 递归列出子目录中的文件
            } else {
                System.out.println("File: " + fileStatus.getPath());
            }
        }
    }
}
